const Service =require('egg').Service
class CarService extends Service {
    async add({
        product_id,
        user_id,
        num=1
    }){
        const item=await this.get( product_id,user_id)
        if(!item){
            const CarItem=await this.app.mysql.insert('car',{
                product_id,
                user_id,
                num
            })
            return CarItem
        }
        const carItem =await this.update({
            ...item,
            num:item.num+1
        })
        return carItem
       
    }
    async get(product_id,user_id){
        const CarItem=await this.app.mysql.get('car',{
            product_id,user_id
        })
        return CarItem
    }
    async update(params){
        const CarItem=await this.app.mysql.update('car',params)
        return CarItem
    }
    async list(user_id){
        const sql=`
        select 
          car.id,
          product.name,
          car.product_id,
          product.price,
          car.num,
          product.cover,
          car.active
        from 
        car 
        left join
         product 
         on 
         car.product_id=product_id
        where
        user_id=${user_id}

        `
        const carList=await this.app.mysql.select(sql)
        return carList
    }
    async delete(id){
        const carList=await this.app.mysql.delete('car',{
           id
        })
        return carList
    }
}
module.exports=CarService